#ifndef _UTILS_H_
#define _UTILS_H_

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <windows.h>

#define ARRAY_SIZE(arr)     (sizeof(arr)/sizeof(arr[0]))
#define CLEAN(x)            memset(&(x), 0, sizeof(x))

#define END         "\e[0m"
#define BLACK       "\e[30m"
#define WHITE       "\e[37m"
#define RED         "\e[31m"
#define GREEN       "\e[32m"
#define YELLOW      "\e[33m"
#define BLUE        "\e[34m"
#define PURPLE      "\e[35m"
#define CYAN        "\e[36m"
#define BBLACK      "\e[1;30m"
#define BWHITE      "\e[1;37m"
#define BRED        "\e[1;31m"
#define BGREEN      "\e[1;32m"
#define BYELLOW     "\e[1;33m"
#define BBLUE       "\e[1;34m"
#define BPURPLE     "\e[1;35m"
#define BCYAN       "\e[1;36m"

#define uierr(fmt, args...)     printf(RED    "[gcom] " fmt END, ## args)
#define uiwarn(fmt, args...)    printf(YELLOW "[gcom] " fmt END, ## args)
#define uiinfo(fmt, args...)    printf(WHITE  "[gcom] " fmt END, ## args)
#define uishow(fmt, args...)    printf(fmt, ## args)


static inline bool is_str_null(char *str)
{
    return (strlen(str) == 0);
}

static inline bool is_str_enter(char *str)
{
    return (strlen(str) == 1 && str[0] == 0x0A);
}

static inline bool is_str_number(char *strbuf, int *num)
{
    int len = strlen(strbuf);
    if (len == 0)
        return false;
    if (strbuf[len - 1] == 0x0A) { // key: LF '\n'
        strbuf[len - 1] = 0;
        if (--len <= 0)
            return false;
    }
    if (strbuf[len - 1] == 0x0D) { // key: CR '\r'
        strbuf[len - 1] = 0;
        if (--len <= 0)
            return false;
    }
    for (int i = 0; i < len; i++)
        if (strbuf[i] < '0' || strbuf[i] > '9')
            return false;
    if (num != NULL)
        *num = atoi(strbuf);
    return true;
}

#endif
